﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using DAL;
using BLL;
namespace Web.Admin 
{
    public partial class NavAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断当前角色对象是否有相应的权限
            Role role = Session["role"] as Role;//从session取角色对象
            if (role != null && role.Authority != null && !role.Authority.Contains("NavAdd"))
            {
                //没权限跳转到提示页面或首页
                Response.Redirect("/Admin/Default.aspx");
            }
            //获取网址参数id
            object id = Request.QueryString["id"];
            if (id != null && !IsPostBack)
            {//修改
                //根据id从数据库取数据
                Nav o = NavBll.SelectOne(int.Parse(id.ToString()));
                //显示到界面
                this.TextBox_Title.Text = o.Title;
                this.TextBox_Href.Text = o.Href;


            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //第一步获取数据
            string Title = this.TextBox_Title.Text;
            string Href = this.TextBox_Href.Text;
            //第二步  创建实体类对想
            Nav obj = new Nav();
            obj.Title = Title; 
            obj.Href = Href;
            //获取网址参数id
            object id = Request.QueryString["id"];
            if (id != null)
            {//修改
                obj.Id = int.Parse(id.ToString());//id赋值
                bool r = NavBll.Update(obj);
                if (r)
                {//修改成功
                    Response.Redirect("/Admin/NavList.aspx");
                }
                else
                {
                    Response.Write("<script>alert('修改失败')</script>");
                }
            }
            else//添加
            { 
                //第三步调用 业务类的添加方法
                bool r = NavBll.Add(obj);
                if (r)
                {//添加成功
                    Response.Redirect("/Admin/NavList.aspx");
                }
                else
                {
                    Response.Write("<script>alert('添加失败')</script>");
                }
            }

        }
    }
}